# !/usr/bin/env python
# -*- coding: utf-8 -*-

import turtle
import math

running = True
size1 = 1
size2 = 2


# 定义一个方形类
class Square:
    def __init__(self, turtle, x, y, size, color):
        self.t = turtle
        self.t.speed(0)
        self.t.shape("square")
        self.t.color(color)
        self.t.resizemode(rmode="user")
        self.t.penup()
        self.t.goto(x, y)
        self.t.shapesize(size, size)

    def move(self, step):
        self.t.forward(step)


# 检测两个方形是否碰撞
def detect_collision(square1, square2):
    print(square1.t.xcor(), square2.t.xcor(), square1.t.ycor(), square2.t.ycor())
    x1 = square1.t.xcor()
    x2 = square2.t.xcor()
    y1 = square1.t.ycor()
    y2 = square2.t.ycor()
    d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    return d <= (size1 * 10 + size2 * 10)


if __name__ == '__main__':
    # 初始化两个方形
    square1 = Square(turtle.Turtle(), 0, 100, size1, "blue")
    square2 = Square(turtle.Turtle(), 50, 100, size2, "red")

    while running:
        # 移动第一个方形
        square1.move(2)
        # 检测是否碰撞
        if detect_collision(square1, square2):
            running = False
            print("碰撞了！")
        # else:
        #     print("没有碰撞。")
    turtle.mainloop()
